package com.fruit.controller.wap.join;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.user.Join;
import com.fruit.service.user.UserManager;
import com.fruit.util.MessagesCode;
import com.fruit.util.ResultInfo;

import jodd.util.StringUtil;



/**
 * 加盟
 * @author rtc
 * @version 2018.3.20 12:20
 */
@Controller
@RequestMapping(value = "/wap/join")
public class JoinController extends BaseController {
	
	@Autowired
	private UserManager userService;

	/**
	 * 加盟 <br/>
	 * @param request request
	 * @param session session
	 * @return 结果
	 * @version 2018.3.20 12:20
	 */
    @RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> saveAddress(HttpServletResponse response, Join join) {
        this.logBefore(logger, "添加加盟信息");
        response.addHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> resMap = new HashMap<String, Object>();
        try {
        	if(join == null || StringUtil.isEmpty(join.getName())){
        		resMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "参数错误!"));
        		return resMap;
        	}
        	resMap=userService.saveJoin(join);
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
        }
        return resMap;
    }
    
    
    /**
     * 收货地址信息
     *
     * @param parentId parentId
     * @return Map<String, Object>
     */
    @RequestMapping(value = "getRegion", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> getRegion(HttpServletResponse response, Integer parentId) {
        this.logBefore(logger, "收货地址信息");
        response.addHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> resMap = new HashMap<String, Object>();
        if (parentId == null) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, MessagesCode.MESS_OUT, "参数不全!"));
            return resMap;
        }
        try {
			List<Map<String, Object>> region = userService.getRegion(parentId);
            if (region.size() > 0) {
            	resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
                resMap.put("region", region);
            } else {
                resMap.put("result", this.initResult(ResultInfo.SUCCESS, MessagesCode.MESS_ISnULL, "未查询到区域!"));
            }
        } catch (Exception e) {
            this.logError(logger, e.getMessage());
            resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "网络繁忙!"));
        }
        return resMap;
    }
    
}
